/**
 * {作业名称}
 *
 * 输入参数:
 *
 * 输出参数:
 *
 */
(() => {
  // 私有区域

  return function ({ Action = 'trigger', Event, Job, ...Data }, headers) {
    // 验证输入参数
    if (!Action) throw Error("操作不能为空");
    if (!Event) throw Error("事件名称不能为空");

    // 处理逻辑
    switch (Action) {
      case 'on': {
        const jobs = A.cache.get(`event.${Event}`)?.split(',') ?? [];
        if (!jobs.includes(Job)) jobs.push(Job);
        A.cache.set(`event.${Event}`, jobs.join(','));
        break;
      }
      case 'one': {
        A.cache.set(`event.${Event}`, Job);
        break;
      }
      case 'off': {
        const jobs = A.cache.get(`event.${Event}`)?.split(',') ?? [];
        if (jobs.includes(Job)) jobs.splice(jobs.indexOf(Job), 1);
        A.cache.set(`event.${Event}`, jobs.join(','));
        break;
      }
      case 'clear': {
        A.cache.delete(`event.${Event}`);
        break;
      }
      case 'trigger': {
        const jobs = A.cache.get(`event.${Event}`)?.split(',') ?? [];
        let result = undefined;
        jobs.forEach(jobName => {
          try {
            result = A.job[jobName](Data, headers);
          } catch (e) {
            Logger.Error(e.stack);
          }
        });
        return result;
      }
      default: {
        throw Error("无效操作");
      }
    }

    // 返回结果
    return true;
  };
})();